#include <iostream>
#include "FloorAndCeil.cpp"
#include "SearchTestHelper.h"


template<typename T>
FloorAndCeil binarySearchFloorAndCeil(T arr[], int n, T key) {

    FloorAndCeil floorAndCeil = FloorAndCeil();

    //arr[l,r]进行查找key
    int l = 0, r = n - 1;

    while (l <= r) {

        int mid = l + (r - l) / 2;

        if (arr[mid] == key) {      //mid 索引位置的元素就是key 直接返回
            floorAndCeil.setCeil(mid);
            floorAndCeil.setFloor(mid);
        }

        if (key < arr[mid]) {   //查找arr[l,mid-1]
            floorAndCeil.setCeil(key);
            r = mid - 1;
        } else {     //查找arr[mid+1,r]
            floorAndCeil.setFloor(key);
            l = mid + 1;
        }
    }
    return floorAndCeil;
}

int main() {

    int arr[10] = {1,2,2,3,3,3,4,5,5,8};
    printArray(arr,10);

    FloorAndCeil f = binarySearchFloorAndCeil(arr, 10, 5);

    cout<<f.getFloor()<<" "<<f.getCeil()<<endl;

    return 0;
}